﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Text.RegularExpressions;
using System.Collections.Generic;



//实现各项功能格式化和统计功能
namespace 源文件格式化与统计
{
    internal class Function
    {
        internal int sum_of_line=0;
        internal int sum_of_words=0;
        internal Dictionary<string, int> Words = new Dictionary<string,int>();
        private string pattern = @"([a-zA-Z]+)([a-zA-Z0-9_]*)";

        //统计行数和单词数
        public void cs_file_read(string filename)
        {
           
            FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);

            StreamReader sr = new StreamReader(fs,System.Text.Encoding.Default);

            while(!sr.EndOfStream)
            {
                string buffer = sr.ReadLine();
                if(buffer == "")
                {
                    continue;//不统计空行
                }
                else
                {
                    sum_of_line++;
                    MatchCollection matches = Regex.Matches(buffer, pattern);
                    for(int i = 0; i < matches.Count; i++)
                    {
                       string word = matches[i].Value;
                        if (Words.ContainsKey(word))
                        {
                            sum_of_words++;
                            Words[word]++;
                        }
                        else
                        {
                            sum_of_words++;
                            Words.Add(word, 1);
                        }

                    }
                }
            }  
            
            //释放资源
            fs.Close();
            sr.Close();
        }

        //对文件格式化
        public void cs_file_format(string filename)
        {
            string[] origin = File.ReadAllLines(filename, System.Text.Encoding.Default);
            StreamWriter sw = new StreamWriter(filename, false, System.Text.Encoding.Default);
            for (int i = 0;i< origin.Length; i++)
            {
                if (origin[i] == "" || Regex.IsMatch(origin[i],@"^[//].+"))
                {
                    //空行和注释不写入
                }
                else
                {
                    sw.WriteLine(origin[i]);
                }                         
            }
            sw.Close();
        }

        public void cs_formatedflie_read(string filename)
        {
            FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);

            StreamReader sr = new StreamReader(fs, System.Text.Encoding.Default);

            while (!sr.EndOfStream)
            {
                string buffer = sr.ReadLine();
                if (buffer == "" || Regex.IsMatch(buffer, @"^[//].+"))
                {
                    continue;//不统计空行和注释
                }
                else
                {
                    sum_of_line++;
                    MatchCollection matches = Regex.Matches(buffer, pattern);
                    for (int i = 0; i < matches.Count; i++)
                    {
                        string word = matches[i].Value;
                        if (Words.ContainsKey(word))
                        {
                            sum_of_words++;
                            Words[word]++;
                        }
                        else
                        {
                            sum_of_words++;
                            Words.Add(word, 1);
                        }

                    }
                }
            }
            fs.Close();
            sr.Close();
        }
    }
}
